package org.monster.context.core.headerfilter;

import org.monster.context.core.parameter.HttpResponse;
import org.monster.context.core.request.HttpRequest;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 头过滤器通道
 *
 * @author Monster
 */
public class HeaderFilterChain extends BaseFilterChain implements FilterChain {

    /**
     * 头过滤器 集合
     */
    private List<HeaderFilter> headerFilters = new ArrayList<>();

    /**
     * 做头过滤
     *
     * @param request  请求
     * @param response 响应
     * @param chain    通道
     */
    public void doHeaderFilter(HttpRequest request, HttpResponse response,
                               HeaderFilterChain chain) {
        for (HeaderFilter headerFilter : getHeaderFilters()) {
            headerFilter.parseHeaders(request, response, chain);
        }
    }

    /**
     * 添加 头过滤
     *
     * @param filter 头过滤器
     */
    public void addHeaderFilter(HeaderFilter filter) {
        getHeaderFilters().add(filter);
    }

    /**
     * 做 过滤
     *
     * @param request  请求
     * @param response 响应
     * @throws IOException      输入输出异常
     * @throws ServletException 服务者异常
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response)
            throws IOException, ServletException {
    }

    /**
     * 获取 头过滤器 集合
     *
     * @return 头过滤器 集合
     */
    public List<HeaderFilter> getHeaderFilters() {
        return headerFilters;
    }
}
